package com.tjtt.tdm.system.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.system.bean.ConfigInfo;
import com.tjtt.tdm.system.dao.ConfigDao;
import com.tjtt.tdm.system.service.ConfigService;

public class ConfigServiceImpl implements ConfigService {
	private static final Log log = LogFactory.getLog(ConfigServiceImpl.class);

	private ConfigDao configDao;
	
	public void setConfigDao(ConfigDao configDao) {
		this.configDao = configDao;
	}

	
	public ConfigInfo getConfigInfoById(Long configId) throws ServiceException {
		
			try {
				if(null != configId && configId.intValue()!=0){
					return this.configDao.get(configId);
				}else{
					List<ConfigInfo> list = this.configDao.getAll();
					if(null != list){
						return list.get(0);
					}
					return null;
				}
			} catch (DaoException e) {
				log.error(e.getMessage());
				throw new ServiceException(e.getMessage(),e);
			}
	}

	
	public void saveOrUpdateConfigInfo(ConfigInfo config) throws ServiceException {
		try {
			this.configDao.saveOrUpdate(config);
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(),e);
		}
		
	}


	public String getFilePath() {
		// TODO Auto-generated method stub
		return null;
	}


	public int getPageNum() {
		// TODO Auto-generated method stub
		return 0;
	}


	
	public ConfigInfo findConfig()
			throws ServiceException {
		ConfigInfo configInfo = null;
		try {
			List<ConfigInfo> configList = configDao.getAll();			
			if (configList != null && configList.size() == 1) {
				configInfo = configList.get(0);
			}
			return configInfo;
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(), e);
		}
	}
	

	@Override
	public String getSeqTdm() throws ServiceException {
		try {
			Long id = configDao.executeSeq("SEQ_TDM");
			return String.valueOf(id);
		} catch (DaoException e) {
			log.error(e.getMessage());
			throw new ServiceException(e.getMessage(), e);
		}
	
	}

}